#!/bin/bash


#30 01 * * *  sh /home/bsmp/work/bin/sds_idc_sitemonitor_2.sh  >/dev/null &

source ~/.bash_profile
source /etc/profile

export ORACLE_HOME=/home/hadoop/oracleclient/instantclient_11_2
export ORACLE_OWNER=oracle
export ORACLE_SID=BSMP
export ORACLE_TERM=xterm
#export LD_ASSUME_KERNEL=2.4.1
export THREADS_FLAG=native
export TNS_ADMIN=/home/hadoop/oracleclient/instantclient_11_2


RUN_HOME=/home/bsmp/work
datapath=${RUN_HOME}/srcdata
STATTIME=`date +"%Y%m%d" --date="-1 day"`
ENDTIME=`date +"%Y%m%d"`

#STATTIME=`date +"%Y%m%d" --date="-2 day"`
#ENDTIME=`date +"%Y%m%d" --date="-1 day"`

if [ ! -d ${datapath} ]; then
  mkdir ${datapath}
fi
if [ ! -d ${RUN_HOME}/log ]; then
  mkdir ${RUN_HOME}/log
fi
if [ ! -d ${RUN_HOME}/bakdata ]; then
  mkdir ${RUN_HOME}/bakdata
fi

echo `date` ' ----------->>> sitemonitor ' ${STATTIME}' start' >> ${RUN_HOME}/log/sitemonitor_trans.log

exportHbaseToFile()
{
	echo `date` ' Export SDS_DOMAIN_PV_D ${STATTIME}' >> ${RUN_HOME}/log/sitemonitor_trans.log

	echo " scan 'SDS_DOMAIN_PV_D', {COLUMNS => ['cf:PV:c(org.apache.hadoop.hbase.util.Bytes).toLong'] , STARTROW => '${STATTIME}',ENDROW => '${ENDTIME}'} " | hbase shell> ${datapath}/SDS_DOMAIN_PV_D-${STATTIME}.txt

	sed '1,6d;$d' ${datapath}/SDS_DOMAIN_PV_D-${STATTIME}.txt > ${datapath}/SDS_DOMAIN_PV_D-${STATTIME}.txt.copy 
	mv -f ${datapath}/SDS_DOMAIN_PV_D-${STATTIME}.txt ${RUN_HOME}/bakdata
	sed '$d' ${datapath}/SDS_DOMAIN_PV_D-${STATTIME}.txt.copy  > ${datapath}/SDS_DOMAIN_PV_D-${STATTIME}.txt
	rm -rf ${datapath}/SDS_DOMAIN_PV_D-${STATTIME}.txt.copy

	echo `date` ' Export SDS_ABN_SESS_STAT_IDC_D ${STATTIME} ' >> ${RUN_HOME}/log/sitemonitor_trans.log

	echo "scan 'SDS_ABN_SESS_STAT_IDC_D',{COLUMNS =>['cf:SESSIONMIDDLE:c(org.apache.hadoop.hbase.util.Bytes).toLong','cf:DOS:c(org.apache.hadoop.hbase.util.Bytes).toLong','cf:CC:c(org.apache.hadoop.hbase.util.Bytes).toLong'], STARTROW =>'${STATTIME}',ENDROW => '${ENDTIME}'}" | hbase shell > ${datapath}/SDS_ABN_SESS_STAT_IDC_D-${STATTIME}.txt

	sed '1,6d;$d' ${datapath}/SDS_ABN_SESS_STAT_IDC_D-${STATTIME}.txt > ${datapath}/SDS_ABN_SESS_STAT_IDC_D-${STATTIME}.txt.copy 
	mv -f ${datapath}/SDS_ABN_SESS_STAT_IDC_D-${STATTIME}.txt ${RUN_HOME}/bakdata
	sed '$d' ${datapath}/SDS_ABN_SESS_STAT_IDC_D-${STATTIME}.txt.copy  > ${datapath}/SDS_ABN_SESS_STAT_IDC_D-${STATTIME}.txt
	rm -rf ${datapath}/SDS_ABN_SESS_STAT_IDC_D-${STATTIME}.txt.copy

	echo `date` ' Export end' >> ${RUN_HOME}/log/sitemonitor_trans.log
}


execABNSESS()
{
	echo `date` ' execABNSESS Start====== ' >> ${RUN_HOME}/log/sitemonitor_trans.log
	abnfile=$1
	while read myline
	do
	if [[ ${myline} =~ "timestamp" ]]
    then
	 linecol=`echo ${myline}|awk -F" "  '{print $1}'`
	 linecolt=`echo ${myline}|awk -F" "  '{print $2}'`
	 linecolv=`echo ${myline}|awk -F" "  '{print $4}'`
	 reporttime=`echo ${linecol} |awk -F"_"  '{print $1}'`
	 idcid=`echo ${linecol} |awk -F"_"  '{print $2}'`
	 ip=`echo ${linecol} |awk -F"_"  '{print $3}'`
	 pv=`echo ${linecolv}|awk -F"="  '{print $2}'`
	 tmptype=`echo ${linecolt}|awk -F":"  '{print $2}'`
	 type=`echo ${tmptype}|awk -F","  '{print $1}'`
	 if [ ${pv} -gt 0 ] ; then
		if [ "${type}" = "DOS" ] ; then
			echo ${ip}"|DDOS|"${pv}"|"${reporttime}"|"${idcid} >> FINISH-${abnfile}
		else
			echo ${ip}"|"${type}"|"${pv}"|"${reporttime}"|"${idcid} >> FINISH-${abnfile}
		fi
		
	 fi
	fi
	done < ${abnfile}

	echo `date` ' execABNSESS end and delete ${abnfile}' >> ${RUN_HOME}/log/sitemonitor_trans.log
	rm -rf ${abnfile}
}


execDOMAINPV()
{
	echo `date` ' execDOMAINPV Start====== ' >> ${RUN_HOME}/log/sitemonitor_trans.log
	domainfile=$1
#	while read myline
#	do
#	if [[ ${myline} =~ "timestamp" ]]
#    then
#	    linecol=`echo ${myline}|awk -F" "  '{print $1}'`
#	    linecolt=`echo ${myline}|awk -F" "  '{print $4}'`
#	    reporttime=`echo ${linecol} |awk -F"_"  '{print $1}'`
#	    idcid=`echo ${linecol} |awk -F"_"  '{print $2}'`
#	    domainname=`echo ${linecol} |awk -F"_"  '{print $3}'`
#	    pv=`echo ${linecolt}|awk -F"="  '{print $2}'`

#	    echo ${reporttime}"|"${pv}"|"${domainname}"|"${idcid} >> FINISH-${domainfile}
#	fi
#	done < ${domainfile}

    sed 's/_/|/g;s/ column=cf:PV, timestamp=/|/g;s/, value=/|/g;s/ //g;'  ${domainfile}> FINISH-${domainfile}

	echo `date` ' execDOMAINPV end and delete ${domainfile}' >> ${RUN_HOME}/log/sitemonitor_trans.log
	rm -rf ${domainfile}
}


exportHbaseToFile

cd ${datapath}
filenames=`ls -l SDS*.txt | awk '{print $9}'`
currentfileno=`ls SDS*.txt -l| wc -l`

if [ '${currenfileno}' = '0' ]; then
	echo `date` ' No file find ! in ' ${datapath} >>${RUN_HOME}/log/sitemonitor_trans.log
	exit 1
else
	echo `date` ' currentfileno is ' ${currentfileno} >>${RUN_HOME}/log/sitemonitor_trans.log
fi

for file in ${filenames}
do
	tablename=`echo ${file}|awk -F"-"  '{print $1}'`
	if [ "${tablename}" = "SDS_ABN_SESS_STAT_IDC_D" ] ; then
		fuser ${file} || execABNSESS ${file}
	else
		fuser ${file} || execDOMAINPV ${file}
	fi
done

echo `date` ' End of Statistics' >> ${RUN_HOME}/log/sitemonitor_trans.log

exit 0
